#include "IntegerSet.h"

IntegerSet::IntegerSet ()
{
}

IntegerSet::IntegerSet (int *a, size_t b)
: m_set (a, a+b)
{
}

IntegerSet IntegerSet::unionOfSets (const IntegerSet & a) const
{
    IntegerSet ret_set (*this);
    // strang vc6 problem here:
    // the first element of set is always invalid
    // so i have to discard it
    for (set_t::const_iterator it = (++a.m_set.begin ()); it != a.m_set.end (); it++)
    {
        ret_set.m_set.insert (*it);
    }

    return ret_set;
}

IntegerSet IntegerSet::intersectionOfSets (const IntegerSet & a) const
{
    IntegerSet ret_set;
    for (set_t::const_iterator it = (++a.m_set.begin ()); it != a.m_set.end (); it++)
    {
        set_t::const_iterator tmp = this->m_set.find (*it);
        if (tmp != this->m_set.end ())
        {
            ret_set.m_set.insert (*it);
        }
    }
    return ret_set;
}

void IntegerSet::emptySet ()
{
    m_set.clear ();
}

void IntegerSet::inputSet (istream & in)
{
    int tmp;
    in >> tmp;
    m_set.insert (tmp);
}

void IntegerSet::insertElement (int a)
{
    m_set.insert (a);
}

void IntegerSet::deleteElement (int a)
{
    m_set.erase (a);
}

void IntegerSet::printSet (ostream & out) const
{
    if (m_set.size () == 1)
        out << "---";
    else
    {
        out << "{ ";
        std::copy ((++m_set.begin ()), m_set.end (), std::ostream_iterator<int> (out, " "));
        out << "}";
    }
}

bool IntegerSet::isEqualTo (const IntegerSet & a) const
{
    return (*this).m_set == a.m_set;
}

const IntegerSet operator& (const IntegerSet & a, const IntegerSet & b)
{
    return a.intersectionOfSets (b);
}

const IntegerSet operator| (const IntegerSet & a, const IntegerSet & b)
{
    return a.unionOfSets (b);
}

bool operator== (const IntegerSet & a, const IntegerSet & b)
{
    return a.isEqualTo (b);
}

bool operator!= (const IntegerSet & a, const IntegerSet & b)
{
    return !(a == b);
}

ostream & operator<< (ostream & out, const IntegerSet & a)
{
    a.printSet (out);
    return out;
}

istream & operator>> (istream & in, IntegerSet & a)
{
    a.inputSet (in);
    return in;
}

IntegerSet & operator<< (IntegerSet & a, int b)
{
    a.insertElement (b);
    return a;
}
